function FV_cmu_training(Ncent, RUN, ORI_DIM, DIM, is_pca )
%%Calcular FV for all videos

fprintf('Ng %d \n',Ncent);

for r=1:RUN
    run = int2str(r);
    Ng = int2str(Ncent);
    dim = int2str(DIM);
    ori_dim = int2str(ORI_DIM);
    fprintf('FV for training data RUN %s \n',run);
    
    if (is_pca)
        
        
        w  =    load(strcat('./run', run, '/universal_GMM/pca_weights_Ng', Ng, 'dim', dim, '.dat' ));
        mu =    load(strcat('./run', run, '/universal_GMM/pca_means_Ng'  , Ng, 'dim', dim, '.dat'  ));
        sigma = load(strcat('./run', run, '/universal_GMM/pca_covs_Ng'   , Ng, 'dim', dim, '.dat'  ));
        trans_matrix_pca = load(strcat('./run', run, '/universal_GMM/pca_transf_matrix_dim', dim, '.dat' ));
        
    else
        %No existe todavia
        w  =    load(strcat('./run', run, '/universal_GMM/weights_Ng', Ng, '_dim', dim, '.dat' ));
        mu =    load(strcat('./run', run, '/universal_GMM/means_Ng'  , Ng, '_dim', dim, '.dat' ));
        sigma = load(strcat('./run', run, '/universal_GMM/covs_Ng'   , Ng, '_dim', dim, '.dat' ));
    end
    
    
    people_train = importdata(strcat('./run',run,'/train_list_run',run,'.dat'));
    actionNames = importdata('actionNames.txt');
    
    
    n_people  = length(people_train);
    n_actions = length(actionNames);
    
    for i=1:n_people
        for j=1:n_actions
            name_feat = strcat('./features_training/feature_vectors_dim',ori_dim, '_', people_train{i},'_',actionNames{j}, '.dat');
            display('*********************');
            fprintf('FV for training data RUN %s \n',run);
            display(strcat( people_train{i},'_',actionNames{j} ));
            S = char(name_feat);
            
            if (exist(S))
                %data_onevideo = load(S);
                %[ di le] = size(data_onevideo);
                one_video = {load(S)};
                [ di le] = size(one_video{1}); % I only have one
                fprintf('le %d \n',le);
                
                %Tal vez esto no lo necesito :)
                %if (le>3000000) %10.000.000
                %   display('Matrix so big');
                %  max_n_frames = 1000000; %floor(le/2);
                % one_video{1,1}(:,max_n_frames:end)=[];
                %end
                
                [di le] = size(one_video{1,1}); % I only have one
                
                
                if ( le>0 )
                    
                    if (is_pca)
                        %size( trans_matrix_pca )
                        %size(one_video{1,1})
                        one_video{1,1} = one_video{1,1}'*trans_matrix_pca;
                        one_video{1,1} = one_video{1,1}';
                        %size(one_video{1,1})
                        %pause()
                    end
                    
                    
                    
                    v = compute_fisher_joha (single(w), single(mu), single(sigma), one_video);
                    clear one_video;
                    d_fisher = size (v, 1);              % dimension of the Fisher vectors
                    % power "normalisation"
                    display('Power Normalisation');
                    v = sign(v) .* sqrt(abs(v));
                    %L2 normalization (may introduce NaN vectors)
                    display('L2 Normalisation');
                    vn = yael_fvecs_normalize (v);
                    clear v;
                    % replace NaN vectors with a large value that is far from everything else
                    % For normalized vectors in high dimension, vector (0, ..., 0) is *close* to
                    % many vectors.
                    %vn(find(isnan(vn))) = 123;
                    if ( length( find( isnan(vn) ) )> 0 )
                        disp('Que hago??????');
                        
                    end
                    
                    %to save
                    
                    if (is_pca)
                        save_name = strcat('./run',run,'/FV_training/pca_FV_', people_train(i),'_',actionNames(j), '_Ng', Ng, '_dim', dim,'.txt');
                        sSave = char(save_name);
                        %display(sSave);
                        fid1=fopen(sSave,'wt');
                        fprintf(fid1,'%8.8f\n',vn);
                        fclose(fid1);
                    else
                        save_name = strcat('./run',run,'/FV_training/FV_', people_train(i),'_',actionNames(j), '_Ng', Ng, '_dim', dim,'.txt');
                        sSave = char(save_name);
                        %display(sSave);
                        fid1=fopen(sSave,'wt');
                        fprintf(fid1,'%8.8f\n',vn);
                        fclose(fid1);
                    end
                    
                end
                clear vn fid1;
            end
        end
    end
end
